import cherrypy
from NovelModule import NovelModule
from BaikeModule import BaikeModule


class NetRoot(object):

    def __init__(self, title, contain):
        """
        网络模块
        :param title:
        :param contain:
        """

        self.novel_title = title
        self.novel_contain = contain
        ...

    @cherrypy.expose
    def read(self, url):
        novel_module = NovelModule()
        res = novel_module.get_content(url)
        content = """<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Roxy Novel Module</title><style>body {margin: 0;padding: 100px 100px 100px 100px;}h1 {font-size: 40px;text-align:center;}p {font-size: 24px;}</style></head><body>"""
        # content += "<h1>" + title + "</h1><br>"
        # print (self.now_content)
        for i in res:
            content += "<p>" + i + "</p>"
        content += "</body></html>"
        return content

    @cherrypy.expose
    def baike(self, key):
        baike_module = BaikeModule()
        url = "https://baike.baidu.com/search/word?fromModule=lemma_search-box&lemmaId=lemma_search-box&word=" + key
        res = baike_module.get_request(url)
        content = """<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Roxy Novel Module</title><style>body {margin: 0;padding: 100px 100px 100px 100px;}h1 {font-size: 40px;}p {font-size: 24px;}h1 {font-size:45px;} h2 {font-size:30}</style></head><body>"""
        content += "<h1>" + res["main_title"] + "</h1>"
        content += "<h2>" + res["sub_title"] + "</h2><br><br>"
        content += "<p>" + res["text"] + "</p>"
        content += "</body></html>"
        return content

    @cherrypy.expose
    def read_novel(self):
        if self.novel_contain == None:
            return "<h1>404 Not Found: 可能未选中章节</h1>"
        contain = """<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Roxy Novel Module</title><style>body {
        margin: 0;padding: 100px 100px 100px 100px;}h1 {font-size: 40px;text-align:center;}p {font-size: 
        24px;}</style></head><body>"""
        contain += "<h1>" + self.novel_title + "</h1>"
        for i in self.novel_contain:
            contain += "<p>" + i + "</p>"
        contain += "</body></html>"
        return contain

class NetworkModule:

    def __init__(self):
        """
        网络模块
        """
        self.network_running = False
        self.step = "ready_to_read"

    def set_route(self):
        ...

    def run_network(self):
        """
        运行Cherrpy
        :return:
        """

        self.network_running = True
        print("Running network at localhost:8009")
        cherrypy.config.update({'server.socket_port': 8009})
        cherrypy.quickstart(NetRoot(), '/')

    def run_novel_network(self, title, contain):
        """
        运行Cherrypy(New Method)
        :param title:
        :param contain:
        :return:
        """

        self.network_running = True
        print("Running network at localhost:8009")
        cherrypy.config.update({'server.socket_port': 8009})
        cherrypy.quickstart(NetRoot(title, contain), '/')



if __name__ == "__main__":
    network = NetworkModule()
    # network.set_route()
    network.run_network()